<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改学生')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-student-edit" th:object="${eduStudent}">
            <h4 class="form-header h4">学生信息</h4>
            <input name="studentId" th:field="*{studentId}" type="hidden">
            <input name="regionId"  type="hidden"  th:field="*{regionId}" id="treeId"/>
            <div class="form-group">
                <label class="col-sm-3 control-label">姓名：</label>
                <div class="col-sm-8">
                    <input name="studentName" th:field="*{studentName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">学号：</label>
                <div class="col-sm-8">
                    <input name="studentCode" th:field="*{studentCode}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">班级：</label>
                <div class="col-sm-8">
                    <select id="klass" th:field="*{klassId}" class="form-control select2-multiple">
                        <option th:each="klass:${klasses}" th:value="${klass.klassId}" th:text="${klass.klassName}" th:selected="${klass.klassId==klassId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">性别：</label>
                <div class="col-sm-8">
                    <select name="sex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{sex}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">籍贯：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="text" name="regionName" onclick="selectRegionTree()" id="treeName" th:field="*{region.regionName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">家庭成员信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/student";
        var sexDatas = [[${@dict.getType('sys_user_sex')}]];
        var relationshipDatas = [[${@dict.getType('edu_student_relationship')}]];
        $("#form-student-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-student-edit').serialize());
            }
        }

        /* 用户管理-修改-选择部门树 */
        function selectRegionTree() {
        	var regionId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();
            var url = ctx + "system/region/selectRegionTree/" + regionId;
		    var options = {
				title: '选择部门',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}

		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}

        $(function() {
            var options = {
                data: [[${eduStudent.eduRelativeList}]],
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'relativeName',
                    align: 'center',
                    title: '姓名',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='eduRelativeList[%s].relativeName' value='%s'>", index, value);
                        return html;
                    }
                },

                {
                    field: 'sex',
                    align: 'center',
                    title: '性别',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("eduRelativeList[%s].sex", index);
                        return $.common.dictToSelect(sexDatas, value, name);
                    }
                },
                {
                    field: 'relationship',
                    align: 'center',
                    title: '关系',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("eduRelativeList[%s].relationship", index);
                        return $.common.dictToSelect(relationshipDatas, value, name);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
            };
            $.table.init(options);
        });

        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                relativeName: "",
                sex: "",
                relationship: "",
            }
            sub.addRow(row);
        }
    </script>
</body>
</html>